#include "userinfo.h"
#include <QHeaderView>

UserInfo::UserInfo(const QString &username, Database *db, QWidget *parent)
    : QWidget(parent)
    , username(username)
    , db(db)
{
    setupUI();
    createConnections();
    setupTableModel();
}

UserInfo::~UserInfo()
{
}

void UserInfo::setupUI()
{
    QVBoxLayout *mainLayout = new QVBoxLayout(this);
    mainLayout->setSpacing(20);
    mainLayout->setContentsMargins(20, 20, 20, 20);

    // 用户信息
    QLabel *welcomeLabel = new QLabel(QString("欢迎, %1").arg(username), this);
    welcomeLabel->setStyleSheet("font-size: 24px; font-weight: bold; color: #2196F3;");
    mainLayout->addWidget(welcomeLabel);

    // 当前借阅列表
    QLabel *borrowLabel = new QLabel("当前借阅", this);
    borrowLabel->setStyleSheet("font-size: 18px; font-weight: bold;");
    mainLayout->addWidget(borrowLabel);

    borrowTableView = new QTableView(this);
    borrowTableView->setStyleSheet("QTableView { border: 1px solid #BDBDBD; border-radius: 4px; }"
                                 "QTableView::item { padding: 8px; }"
                                 "QHeaderView::section { background-color: #2196F3; color: white; padding: 8px; }");
    borrowTableView->setSelectionBehavior(QAbstractItemView::SelectRows);
    borrowTableView->setSelectionMode(QAbstractItemView::SingleSelection);
    borrowTableView->setAlternatingRowColors(true);
    mainLayout->addWidget(borrowTableView);

    // 按钮
    QHBoxLayout *buttonLayout = new QHBoxLayout();
    buttonLayout->setSpacing(20);

    changePasswordButton = new QPushButton("修改密码", this);
    historyButton = new QPushButton("历史记录", this);

    QString buttonStyle = "QPushButton { padding: 8px 16px; border: none; border-radius: 4px; }"
                         "QPushButton:hover { opacity: 0.8; }"
                         "QPushButton:pressed { opacity: 0.6; }";

    changePasswordButton->setStyleSheet(buttonStyle + "background-color: #FFC107; color: black;");
    historyButton->setStyleSheet(buttonStyle + "background-color: #2196F3; color: white;");

    buttonLayout->addWidget(changePasswordButton);
    buttonLayout->addWidget(historyButton);
    mainLayout->addLayout(buttonLayout);

    // 设置窗口样式
    setStyleSheet("QWidget { background-color: white; }");
}

void UserInfo::createConnections()
{
    connect(changePasswordButton, &QPushButton::clicked, this, &UserInfo::onChangePasswordClicked);
    connect(historyButton, &QPushButton::clicked, this, &UserInfo::onHistoryClicked);
}

void UserInfo::setupTableModel()
{
    borrowModel = new QSqlTableModel(this);
    borrowModel->setTable("borrow_records");
    borrowModel->setEditStrategy(QSqlTableModel::OnManualSubmit);

    // 设置表头
    borrowModel->setHeaderData(0, Qt::Horizontal, "借阅ID");
    borrowModel->setHeaderData(1, Qt::Horizontal, "图书ID");
    borrowModel->setHeaderData(2, Qt::Horizontal, "借阅日期");
    borrowModel->setHeaderData(3, Qt::Horizontal, "状态");

    borrowTableView->setModel(borrowModel);
    borrowTableView->horizontalHeader()->setSectionResizeMode(QHeaderView::Stretch);
    borrowTableView->verticalHeader()->setVisible(false);

    // 只显示当前用户的借阅记录
    borrowModel->setFilter(QString("user_id = (SELECT id FROM users WHERE username = '%1') AND status = 'borrowed'")
                         .arg(username));
    borrowModel->select();
}

void UserInfo::onChangePasswordClicked()
{
    ChangePasswordWindow *changeWindow = new ChangePasswordWindow(username, db, this);
    changeWindow->exec();
}

void UserInfo::onHistoryClicked()
{
    HistoryWindow *historyWindow = new HistoryWindow(username, db, this);
    historyWindow->exec();
} 